{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7e6c96b-9d1d-4007-a556-ba1c5a93cfe6",
   "metadata": {},
   "source": [
    "你可以像定义 Query 参数和 Path 参数一样来定义 Cookie 参数。\n",
    "\n",
    "首先，导入 Cookie:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "15d5df65-16ec-4f8b-8581-b5d57df223d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [20348]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:55519 - \"GET /docs HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55519 - \"GET /openapi.json HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55518 - \"GET /items/ HTTP/1.1\" 422 Unprocessable Entity\n",
      "INFO:     127.0.0.1:55572 - \"GET /items/ HTTP/1.1\" 422 Unprocessable Entity\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [20348]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from typing import Annotated\n",
    "from fastapi import Cookie, FastAPI\n",
    "app = FastAPI()\n",
    "@app.get(\"/items/\")\n",
    "async def read_items(ads_id: Annotated[str | None, Cookie()]):\n",
    "    return {\"ads_id\": ads_id}\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "815ab867-5edc-4a26-83cc-4a00fa27b897",
   "metadata": {},
   "source": [
    "声明 Cookie 参数的结构与声明 Query 参数和 Path 参数时相同。\n",
    "\n",
    "你需要使用 Cookie 来声明 cookie 参数，否则参数将会被解释为查询参数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c90a87f-49fb-4da9-b217-3e577dc37064",
   "metadata": {},
   "source": [
    "## requests库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76404f49-912a-43d4-80dc-d97693ea2ac7",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/items/' \n",
    "headers = {\"Cookie\":\"cka=111a;ckb=111b\"}\n",
    "res = requests.get(url, headers=headers) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 '{\"ads_id\":\"你好\"}'\n",
    "\n",
    "# 不知为什么，无法识别Cookie，需要再探索探索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3527b0b7-187c-43db-a042-193454c6df48",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
